package com.hugoo.imp.buzz.mo;

import com.hugoo.core.util.TL;

/**
 *
 * @author Lovelock.Chan
 */
public class ColumnInfo {

    private String name, desc, rules, type, deval, isNull;
    private int maxLength, minLength;
    private boolean nullable, exists = false;
    private boolean vlMin, vlMax = true, vlPat;

    public String getIsNull() {
        return minLength > 0 ? "否" : "是";
    }

    @Override
    public String toString() {
        return "ColumnInfo{" + "name=" + name + ", desc=" + desc + ", rules=" + rules + ", type=" + type + ", deval=" + deval + ", isNull=" + isNull + ", maxLength=" + maxLength + ", minLength=" + minLength + ", nullable=" + nullable + ", exists=" + exists + ", vlMin=" + vlMin + ", vlMax=" + vlMax + ", vlPat=" + vlPat + '}';
    }

    /**
     * 定规则： MaL:30 指定最大值 MiL:0 指定最小值 PaT:《正则表达式》 指定验证格式
     *
     * @param vl
     * @return
     */
    public void init_aLinfo(String vl) {
        if (TL.isEmpty(vl)) {
            return;
        }
        String[] v = vl.split(",");
        int a = -1, i = -1;
        String p = null;
        for (String l : v) {
            if (l.startsWith("MaL")) {//最大长度
                a = TL.parseInt(l);
                vlMin = a > 0;
            } else if (l.startsWith("MiL")) {//最小长度
                i = TL.parseInt(l);
            } else if (l.startsWith("PaT")) {//自定义规则
                p = l.substring(l.indexOf(":"));
                vlPat = TL.isNotEmpty(p);
            } else if (l.startsWith("TyP")) {//定义类别
                String[] vx = l.split(":");
                if (vx.length > 1) {
                    if ("NUM".equalsIgnoreCase(vx[1])) {
                        this.type = "NUMBER";
                        this.vlMax = false;
                    } else if ("DATE".equalsIgnoreCase(vx[1])) {
                        this.type = "DATE";
                        this.vlMax = false;
                    } else if ("VARCHAR".equalsIgnoreCase(vx[1])) {
                        if (vx.length > 2) {
                            this.maxLength = TL.parseInt(vx[2]);
                            this.vlMax = true;
                        }
                    }
                }
            }
        }
        this.setAltInfo(i, a, p);
    }

    public ColumnInfo(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public ColumnInfo(String name, String desc, String defVal) {
        this.name = name;
        this.desc = desc;
        this.deval = defVal;
    }

    public void setAltInfo(int min, int max, String rules) {
        this.minLength = min;
        this.maxLength = max > maxLength ? maxLength : max;//两者间取最小值
        this.rules = rules;
    }

    public void setAltInfo(String type, String length, String deval, String nullable) {
        this.maxLength = Integer.parseInt(length);
        this.type = type;
        this.deval = deval;
        if ("false".equalsIgnoreCase(nullable)) {
            this.minLength = 1;//若数据说明不能为空，那系统自动设置为最小值为1；
            vlMin = true;
        }
        this.exists = true;//表示不用创建
    }

    public boolean isVlMin() {
        return vlMin;
    }

    public boolean isVl() {
        return vlMin || vlMax || vlPat;
    }

    public boolean isVlMax() {
        return vlMax;
    }

    public boolean isVlPat() {
        return vlPat;
    }

    public String getDeval() {
        return deval;
    }

    public boolean exists() {
        return exists;
    }

    public boolean isNullable() {
        return minLength > 0;
    }

    public String getDesc() {
        return desc;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public int getMinLength() {
        return minLength;
    }

    public String getName() {
        return name;
    }

    public String getRules() {
        return rules;
    }

    public String getType() {
        return type;
    }
}
